package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.DictionaryItem;
import cn.wolfcode.crm.query.DictionaryItemQueryObject;
import cn.wolfcode.crm.service.IDictionaryItemService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("systemDictionaryItem")
public class DictionaryItemController {
    @Autowired
    private IDictionaryItemService dictionaryItemService;

    /**
     * 用于系统管理数据字典显示(带分页)
     *
     * @param qo
     * @return
     */
    @RequiresPermissions(value = {"systemDictionaryItem:query", "系统字典明细列表"}, logical = Logical.OR)
    @RequestMapping("query")
    @ResponseBody
    public PageResult query(DictionaryItemQueryObject qo) {
        return dictionaryItemService.query(qo);
    }

    /**
     * 用于关联其它表格回去字典明细数据(无分页)
     *
     * @param sn (字典目录编码)
     * @return
     */
    @RequiresPermissions(value = {"systemDictionaryItem:getItemBySn", "系统字典明细列表"}, logical = Logical.OR)
    @RequestMapping("getItemBySn")
    @ResponseBody
    public List<DictionaryItem> getItemBySn(String sn) {
        List<DictionaryItem> itemBySn = dictionaryItemService.getItemBySn(sn);
        return dictionaryItemService.getItemBySn(sn);
    }

    @RequiresPermissions(value = {"dictionaryItem:saveOrUpdate", "系统字典明细编辑/新增"}, logical = Logical.OR)
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(DictionaryItem entity) {
        try {
            dictionaryItemService.saveOrUpdate(entity);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult().mark("操作失败!原因:" + e.getMessage());
        }
    }

    @RequiresPermissions(value = {"dictionaryItem:delete", "系统字典明细删除"}, logical = Logical.OR)
    @RequestMapping("delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        try {
            dictionaryItemService.deleteByPrimaryKey(id);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult().mark("操作失败!原因:" + e.getMessage());
        }
    }

}
